/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyOrientedServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-23 下午1:16:17
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.ShopCompanyModel;
import com.ywt.cda.action.model.ShopCompanyOrientedModel;
import com.ywt.cda.action.vo.ShopCompanyVo;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.dao.Ana2ndDataDao;
import com.ywt.cda.dao.AnaImportDataDao;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.ConfParam;
import com.ywt.cda.domain.SCategory;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.domain.enums.ParamFlag;
import com.ywt.cda.service.ConfDataCacheService;
import com.ywt.cda.service.ShopCompanyOrientedService;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-23 下午1:16:17
 */
@Service
public class ShopCompanyOrientedServiceImpl implements

ShopCompanyOrientedService {
	@Resource
	private AnaImportDataDao importDataDao;
	@Resource
	private Ana2ndDataDao ana2ndDataDao;
	@Resource
	private ConfDataCacheService cdc;
	@Value("#{configProperties['shop.company.amount']}")
	private String shopAmount;
	
	private final String YYYY_MM = "yyyy-MM";

	@Override
	public void setJsonShops(JSONObject object, String keyword) {
		// TODO Auto-generated method stub

		Map<String, ConfParam> shopCompanys = cdc
				.getMap(ParamFlag.SHOP_COMPANY);
		JSONArray array = new JSONArray();
		Iterator<Entry<String, ConfParam>> iterator = shopCompanys.entrySet()
				.iterator();
		int counter=1;
		while (iterator.hasNext()) {
			Entry<String, ConfParam> entry = iterator.next();
			ConfParam shop = entry.getValue();
			if(counter>Integer.valueOf(shopAmount)){
				break;
			}
			if (shop.getCode().contains(keyword)
					|| shop.getName().contains(keyword)) {
				JSONObject temp = new JSONObject();
				temp.put("code", shop.getCode());
				temp.put("name", shop.getName());
				array.add(temp);
				counter++;
			}
			
		}
		object.put("shops", array);
	}

	@Override
	public List<ShopCompanyVo> toVo(List<AnaImportData> datas) {
		List<ShopCompanyVo> vos = new ArrayList<ShopCompanyVo>();
		if (null != datas && datas.size() > 0) {
			for (AnaImportData data : datas) {
				ShopCompanyVo vo = new ShopCompanyVo();
				String shopName = cdc.getName(ParamFlag.SHOP_COMPANY,
						data.getDimensionCode());
				vo.setName(shopName);
				vo.setCode(data.getDimensionCode());
				vos.add(vo);
			}
		}
		// 如果最近没有电商数据，则随机取一家电商公司
		if (vos.size() == 0) {
			Map<String, ConfParam> shopCompanys = cdc
					.getMap(ParamFlag.SHOP_COMPANY);
			Set<String> keySet = shopCompanys.keySet();
			int random = RandomUtils.nextInt(0, keySet.size() - 1);
			Object[] arrObjects = keySet.toArray();
			String key = (String) arrObjects[random];
			ConfParam param = shopCompanys.get(key);
			ShopCompanyVo vo = new ShopCompanyVo();
			String shopName = cdc.getName(ParamFlag.SHOP_COMPANY,
					param.getCode());
			vo.setName(shopName);
			vo.setCode(param.getCode());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public List<AnaImportData> getShopData(ShopCompanyModel shopCompanyModel,
			List<ShopCompanyVo> datas) {
		AnaTable table = AnaTable.TB_ANA_IMPORT_DATA_D;
		if (YYYY_MM.equals(shopCompanyModel.getStartTimePattern())) {

			table = AnaTable.TB_ANA_IMPORT_DATA_M;
		}

		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", shopCompanyModel.getStartTime_F());
		params.put("endDate", shopCompanyModel.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("dimensionFlag", DimensionFlag.SHOPS_COMPANY.getCode());
		List<String> dimensionCodes = new ArrayList<String>();
		for (ShopCompanyVo vo : datas) {
			dimensionCodes.add(vo.getCode());
		}
		params.put("dimensionCodes", dimensionCodes);

		List<AnaImportData> list = importDataDao.listData(params);

		return list;
	}

	@Override
	public void toZhexianJson(JSONObject objectZhexian,
			List<AnaImportData> shopDatas, ShopCompanyModel shopCompanyModel,
			List<ShopCompanyVo> vos, OrderByEnum orderByEnum) {
		Set<String> shopName = new HashSet<String>();
		Set<String> shopCode = new HashSet<String>();
		for (AnaImportData an : shopDatas) {
			shopName.add(cdc.getName(ParamFlag.SHOP_COMPANY, an
					.getDimensionCode().trim()));
			shopCode.add(an.getDimensionCode());
		}
		JSONArray legendArray = new JSONArray();

		for (ShopCompanyVo vo : vos) {
			String name = vo.getName().trim();
			String code = vo.getCode().trim();
			if (StringUtils.isBlank(name)) {
				name = cdc.getName(ParamFlag.SHOP_COMPANY, vo.getCode());
			}
			if (StringUtils.isBlank(name)) {
				name = vo.getCode();
			}
			if (!shopName.contains(name)) {
				shopName.add(name);
			}
			if (!shopCode.contains(code)) {
				shopCode.add(code);
			}
		}
		for (String shop : shopName) {
			legendArray.add(shop);
		}
		// 按企业分组
		Map<String, List<AnaImportData>> dataMap = new HashMap<String, List<AnaImportData>>();
		for (AnaImportData d : shopDatas) {
			String dimensionCode = d.getDimensionCode().trim();
			if (!dataMap.containsKey(dimensionCode)) {
				List<AnaImportData> temp = new ArrayList<AnaImportData>();
				temp.add(d);
				dataMap.put(dimensionCode, temp);
			}
			else {
				dataMap.get(dimensionCode).add(d);
			}
		}
		for (String shCode : shopCode) {
			if (!dataMap.containsKey(shCode)) {
				List<AnaImportData> temp = new ArrayList<AnaImportData>();

				dataMap.put(shCode, temp);
			}

		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		List<String> dateList = null;
		if (null != shopCompanyModel.getTimeSection()) {
			switch (shopCompanyModel.getTimeSection()) {

			case SIXMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 5),
						specifiedMonth);

				break;
			case NINEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 8),
						specifiedMonth);

				break;
			case TWELVEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 11),
						specifiedMonth);

				break;
			default:
				dateList = DateUtils.getDateList(
						shopCompanyModel.getStartTime_F(),
						shopCompanyModel.getEndTime_F());
				break;
			}
		}
		else {
			dateList = DateUtils.getDateList(shopCompanyModel.getStartTime_F(),
					shopCompanyModel.getEndTime_F());
		}
		JSONArray nameArray = new JSONArray();
		for (String date : dateList) {
			nameArray.add(date);
		}
		Iterator<Entry<String, List<AnaImportData>>> iterator = dataMap
				.entrySet().iterator();
		// 票数
		JSONArray arrayTotal = new JSONArray();
		// 金额
		JSONArray array1Total = new JSONArray();
		// 商品件数
		JSONArray array2Total = new JSONArray();

		while (iterator.hasNext()) {
			// 每个电商对应时间段的数据
			Entry<String, List<AnaImportData>> item = iterator.next();
			String key = item.getKey();
			String name = cdc.getName(ParamFlag.SHOP_COMPANY, key);

			if (StringUtils.isBlank(name)) {
				name = key;
			}
			List<AnaImportData> value = item.getValue();
			JSONObject ojb = new JSONObject();
			JSONObject ojb1 = new JSONObject();
			JSONObject ojb2 = new JSONObject();

			Map<String, AnaImportData> temp = new TreeMap<String, AnaImportData>();
			for (AnaImportData a : value) {
				if (!temp.containsKey(a.getDateFlag())) {
					temp.put(a.getDateFlag(), a);
				}
			}

			// 补全数据
			for (String date : dateList) {
				if (null == temp.get(date)) {
					AnaImportData d = new AnaImportData();
					d.setSumNum(new BigDecimal(0));
					d.setSumPrice(new BigDecimal(0));
					d.setSumPkg(new BigDecimal(0));
					d.setAvgPrice(new BigDecimal(0));
					temp.put(date, d);
				}
			}

			JSONObject obj0 = new JSONObject();
			JSONObject obj01 = new JSONObject();
			JSONObject obj02 = new JSONObject();

			JSONObject obj2 = new JSONObject();
			JSONObject obj21 = new JSONObject();
			JSONObject obj22 = new JSONObject();

			JSONObject obj3 = new JSONObject();
			JSONObject obj3_1 = new JSONObject();
			JSONObject obj3_2 = new JSONObject();

			JSONObject obj31 = new JSONObject();
			JSONObject obj31_1 = new JSONObject();
			JSONObject obj31_2 = new JSONObject();
			JSONObject obj4 = new JSONObject();
			JSONObject obj41 = new JSONObject();
			JSONObject obj42 = new JSONObject();

			JSONObject obj5 = new JSONObject();
			JSONObject obj51 = new JSONObject();
			JSONObject obj52 = new JSONObject();
			String r = getRandom();
			String g = getRandom();
			String b = getRandom();
			String rgb = r + "," + g + "," + b;
			obj4.put("color", "rgba(" + rgb + ",0.5");
			obj41.put("color", "rgba(" + rgb + ",0.5");
			obj42.put("color", "rgba(" + rgb + ",0.5");
			obj3.put("show", true);
			obj3_1.put("show", true);
			obj3_2.put("show", true);
			obj31.put("show", true);
			obj31_1.put("show", true);
			obj31_2.put("show", true);

			obj5.put("label", obj31);
			obj51.put("label", obj31_1);
			obj52.put("label", obj31_2);

			obj2.put("label", obj3);
			obj2.put("areaStyle", obj4);

			obj2.put("color", "rgba(" + rgb + ",1");

			obj21.put("label", obj3_1);
			obj21.put("areaStyle", obj41);
			obj21.put("color", "rgba(" + rgb + ",1");

			obj22.put("label", obj3_2);
			obj22.put("areaStyle", obj42);
			obj22.put("color", "rgba(" + rgb + ",1");

			obj0.put("emphasis", obj5);
			obj0.put("normal", obj2);
			obj01.put("emphasis", obj51);
			obj01.put("normal", obj21);
			obj02.put("emphasis", obj52);
			obj02.put("normal", obj22);

			ojb.put("itemStyle", obj0);
			ojb.put("name", name);

			ojb.put("type", "line");

			

			ojb1.put("itemStyle", obj01);
			ojb1.put("name", name);

			ojb1.put("type", "line");

			

			ojb2.put("itemStyle", obj02);
			ojb2.put("name", name);

			ojb2.put("type", "line");

			

			Iterator<Entry<String, AnaImportData>> iteratorTemp = temp
					.entrySet().iterator();
			// 票数
			JSONArray array = new JSONArray();
			// 金额
			JSONArray array1 = new JSONArray();
			// 商品件数
			JSONArray array2 = new JSONArray();
			while (iteratorTemp.hasNext()) {
				Entry<String, AnaImportData> i = iteratorTemp.next();
				AnaImportData d = i.getValue();
				array.add(d.getSumNum());
				array1.add(d.getSumPrice_());
				array2.add(d.getSumPkg_());
			}
			ojb.put("data", array);
			ojb1.put("data", array1);
			ojb2.put("data", array2);
			arrayTotal.add(ojb);
			array1Total.add(ojb1);
			array2Total.add(ojb2);
		}
		switch (orderByEnum) {
		case SUM_NUM:
			objectZhexian.put("zhexiandata", arrayTotal);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		case SUM_PRICE:
			objectZhexian.put("zhexiandata", array1Total);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		case SUM_PKG:
			objectZhexian.put("zhexiandata", array2Total);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		default:
			break;
		}

	}

	@Override
	public List<Ana2ndData> getDatas(ShopCompanyModel shopCompanyModel,
			List<ShopCompanyVo> vos, DimensionFlag flag,Page page) {

		AnaTable table = AnaTable.TB_ANA_DATA_IMPORT_2ND_SHOP_D;
		if (YYYY_MM.equals(shopCompanyModel.getStartTimePattern())) {

			table = AnaTable.TB_ANA_DATA_IMPORT_2ND_SHOP_M;
		}

		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", shopCompanyModel.getStartTime_F());
		params.put("endDate", shopCompanyModel.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("code", vos.get(0).getCode());
		params.put("mainFlag", flag.getCode());

		List<Ana2ndData> datas = importDataDao.listShopData(params,page);

		return datas;
	}

	@Override
	public void toOtherGraphJson(JSONObject objectOtherGraph,
			List<Ana2ndData> pieDatas, List<Ana2ndData> mapDatas,
			List<Ana2ndData> nianDatas, List<ShopCompanyVo> vos) {
		JSONObject pieData = new JSONObject();

		JSONObject mapData = new JSONObject();
		JSONArray pieLegendArray = new JSONArray();
		JSONArray pieDataArray = new JSONArray();
		Set<String> categoryNames = new HashSet<String>();
		Map<String, Ana2ndData> dataMap = new HashMap<String, Ana2ndData>();

		for (Ana2ndData an : pieDatas) {
			String code = an.getMainCode().trim();
			SCategory sCategory = cdc.getsCategoryMap().get(code);
			String name = "";
			if (null == sCategory) {
				name = code;
			}
			else {
				name = sCategory.getsCategoryName();
			}
			categoryNames.add(name);
			if (!dataMap.containsKey(name)) {
				dataMap.put(name, an);
			}
		}

		for (String shop : categoryNames) {
			pieLegendArray.add(shop);
		}

		Iterator<Entry<String, Ana2ndData>> iterator = dataMap.entrySet()
				.iterator();
		while (iterator.hasNext()) {
			Entry<String, Ana2ndData> entry = iterator.next();
			JSONObject tempObject = new JSONObject();
			tempObject.put("name", entry.getKey());
			tempObject.put("value", entry.getValue().getSumNum());
			pieDataArray.add(tempObject);
		}

		pieData.put("pieLegendArray", pieLegendArray);
		pieData.put("pieDataArray", pieDataArray);

		JSONArray mapDataArray = new JSONArray();

		if (null != mapDatas && mapDatas.size() > 0) {
			// 先排序
			Collections.sort(mapDatas);
			int lenth = mapDatas.size();
			// 0
			Ana2ndData first = mapDatas.get(0);
			Ana2ndData end = mapDatas.get(lenth - 1);
			mapData.put("max", first.getSumNum());
			mapData.put("min", end.getSumNum());
			for (Ana2ndData an : mapDatas) {
				JSONObject objectTmp = new JSONObject();
				objectTmp.put("name", an.getMainCode());
				objectTmp.put("value", an.getSumNum());
				mapDataArray.add(objectTmp);
			}
			mapData.put("mapDataArray", mapDataArray);
		}
		if (null != nianDatas && nianDatas.size() > 0) {
			// 公司名称
			Set<String> shopNames = new HashSet<String>();
			Set<String> shopCode = new HashSet<String>();

			// 每个公司的数据
			Map<String, List<Ana2ndData>> nianDataMap = new HashMap<String, List<Ana2ndData>>();
			for (Ana2ndData data : nianDatas) {
				String code = data.getCode().trim();
				if (nianDataMap.containsKey(code)) {
					nianDataMap.get(code).add(data);
				}
				else {
					List<Ana2ndData> temp = new ArrayList<Ana2ndData>();
					temp.add(data);
					nianDataMap.put(code, temp);
				}
				shopNames.add(cdc.getName(ParamFlag.SHOP_COMPANY, code));
				shopCode.add(code);
			}
			for (ShopCompanyVo vo : vos) {
				String name = vo.getName().trim();
				String code = vo.getCode().trim();
				if (StringUtils.isBlank(name)) {
					name = cdc.getName(ParamFlag.SHOP_COMPANY, vo.getCode());
				}
				if (StringUtils.isBlank(name)) {
					name = vo.getCode();
				}
				if (!shopNames.contains(name)) {
					shopNames.add(name);
				}
				if (!shopCode.contains(code)) {
					shopCode.add(code);
				}
			}
			JSONArray legendArray = new JSONArray();
			for (String shopName : shopNames) {
				legendArray.add(shopName);
			}
			for (String shCode : shopCode) {
				if (!nianDataMap.containsKey(shCode)) {
					List<Ana2ndData> temp = new ArrayList<Ana2ndData>();

					nianDataMap.put(shCode, temp);
				}

			}
			Map<String, Integer> indicatorMap = new TreeMap<String, Integer>();

			Iterator<Entry<String, List<Ana2ndData>>> iterator2 = nianDataMap
					.entrySet().iterator();
			List<Integer> point0 = new ArrayList<Integer>();
			List<Integer> point1 = new ArrayList<Integer>();
			List<Integer> point2 = new ArrayList<Integer>();
			List<Integer> point3 = new ArrayList<Integer>();
			List<Integer> point4 = new ArrayList<Integer>();
			List<Integer> point5 = new ArrayList<Integer>();
			JSONArray dataArray = new JSONArray();
			while (iterator2.hasNext()) {
				Entry<String, List<Ana2ndData>> entry = iterator2.next();
				// 对每家企业进行分析
				JSONObject object = new JSONObject();
				object.put("name",
						cdc.getName(ParamFlag.SHOP_COMPANY, entry.getKey()));
				JSONArray array = new JSONArray();
				Map<String, Integer> indicatorMapTemp = new TreeMap<String, Integer>();
				indicatorMapTemp.put("1", 0);
				indicatorMapTemp.put("2", 0);
				indicatorMapTemp.put("3", 0);
				indicatorMapTemp.put("4", 0);
				indicatorMapTemp.put("5", 0);
				indicatorMapTemp.put("6", 0);
				for (Ana2ndData d : entry.getValue()) {
					if (d.getCountUser() == 1) {
						Integer integer = indicatorMapTemp.get("1");
						integer = Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("1", integer);

					}
					if (d.getCountUser() == 2) {
						Integer integer = indicatorMapTemp.get("2");
						integer = Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("2", integer);
					}
					if (d.getCountUser() == 3) {
						Integer integer = indicatorMapTemp.get("3");
						integer = Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("3", integer);
					}
					if (d.getCountUser() > 3 && d.getCountUser() < 10) {
						Integer integer = indicatorMapTemp.get("4");
						integer = integer + Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("4", integer);
					}
					if (d.getCountUser() == 10) {
						Integer integer = indicatorMapTemp.get("5");
						integer = Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("5", integer);
					}
					if (d.getCountUser() > 10) {
						Integer integer = indicatorMapTemp.get("6");
						integer = integer + Integer.valueOf(d.getMainCode());
						indicatorMapTemp.put("6", integer);
					}
				}
				Iterator<Entry<String, Integer>> iterator3 = indicatorMapTemp
						.entrySet().iterator();
				while (iterator3.hasNext()) {
					Entry<String, Integer> entry2 = iterator3.next();
					array.add(entry2.getValue());

				}
				object.put("value", array);
				dataArray.add(object);
				Integer integer = indicatorMapTemp.get("1");
				Integer integer1 = indicatorMapTemp.get("2");
				Integer integer2 = indicatorMapTemp.get("3");
				Integer integer3 = indicatorMapTemp.get("4");
				Integer integer4 = indicatorMapTemp.get("5");
				Integer integer5 = indicatorMapTemp.get("6");
				point0.add(integer);
				point1.add(integer1);
				point2.add(integer2);
				point3.add(integer3);
				point4.add(integer4);
				point5.add(integer5);
			}
			indicatorMap.put("1", Collections.max(point0));
			indicatorMap.put("2", Collections.max(point1));
			indicatorMap.put("3", Collections.max(point2));
			indicatorMap.put("4", Collections.max(point3));
			indicatorMap.put("5", Collections.max(point4));
			indicatorMap.put("6", Collections.max(point5));
			JSONArray indicatorArray = new JSONArray();

			JSONObject temp = new JSONObject();
			JSONObject temp1 = new JSONObject();
			JSONObject temp2 = new JSONObject();
			JSONObject temp3 = new JSONObject();
			JSONObject temp4 = new JSONObject();
			JSONObject temp5 = new JSONObject();
			temp.put("text", "1次");
			temp.put("max", indicatorMap.get("1"));
			temp1.put("text", "2次");
			temp1.put("max", indicatorMap.get("2"));
			temp2.put("text", "3次");
			temp2.put("max", indicatorMap.get("3"));
			temp3.put("text", "4-9次");
			temp3.put("max", indicatorMap.get("4"));
			temp4.put("text", "10次");
			temp4.put("max", indicatorMap.get("5"));
			temp5.put("text", "大于10次");
			temp5.put("max", indicatorMap.get("6"));
			indicatorArray.add(temp);
			indicatorArray.add(temp1);
			indicatorArray.add(temp2);
			indicatorArray.add(temp3);
			indicatorArray.add(temp4);
			indicatorArray.add(temp5);
			JSONObject nianData = new JSONObject();
			nianData.put("lengendArray", legendArray);
			nianData.put("indicatorArray", indicatorArray);
			nianData.put("dataArray", dataArray);
			objectOtherGraph.put("nianData", nianData);

		}
		objectOtherGraph.put("pieData", pieData);
		objectOtherGraph.put("mapData", mapData);
	}

	private static String getRandom() {
		return String.valueOf(RandomUtils.nextInt(1, 254));
	}

	public static void main(String[] args) {
		System.out.println(getRandom());
		Map<String, Integer> indicatorMapTemp = new TreeMap<String, Integer>();
		indicatorMapTemp.put("1", 0);
		indicatorMapTemp.put("2", 0);
		indicatorMapTemp.put("3", 0);
		indicatorMapTemp.put("4", 0);
		indicatorMapTemp.put("5", 0);
		indicatorMapTemp.put("6", 0);
		Integer integer = indicatorMapTemp.get("3");
		integer = 5;
		// indicatorMapTemp.put("3", integer);
		System.out.println(indicatorMapTemp);
	}

	@Override
	public List<ShopCompanyVo> toVo(
			ShopCompanyOrientedModel shopCompanyOrientedModel) {

		List<ShopCompanyVo> vos = new ArrayList<ShopCompanyVo>();
		List<String> codesList = shopCompanyOrientedModel.getCompanyCode();
		if (null != codesList && codesList.size() > 0) {
			for (String data : codesList) {
				if (StringUtils.isBlank(data)) {
					continue;
				}
				ShopCompanyVo vo = new ShopCompanyVo();

				String shopName = cdc.getName(ParamFlag.SHOP_COMPANY, data);
				vo.setName(shopName);
				vo.setCode(data);
				vos.add(vo);
			}
		}

		return vos;
	}

	@Override
	public List<ShopCompanyVo> toVos(List<Ana2ndData> datas) {
		List<ShopCompanyVo> vos = new ArrayList<ShopCompanyVo>();
		if (null != datas && datas.size() > 0) {
			for (Ana2ndData data : datas) {

				ShopCompanyVo vo = new ShopCompanyVo();
				String shopName = cdc.getName(ParamFlag.SHOP_COMPANY,
						data.getMainCode());
				vo.setName(shopName);
				vo.setCode(data.getMainCode());
				vos.add(vo);
			}
		}
		// 如果最近没有电商数据，则随机取一家电商公司
		if (vos.size() == 0) {
			Map<String, ConfParam> shopCompanys = cdc
					.getMap(ParamFlag.SHOP_COMPANY);
			Set<String> keySet = shopCompanys.keySet();
			int random = RandomUtils.nextInt(0, keySet.size() - 1);
			Object[] arrObjects = keySet.toArray();
			String key = (String) arrObjects[random];
			ConfParam param = shopCompanys.get(key);
			ShopCompanyVo vo = new ShopCompanyVo();
			String shopName = cdc.getName(ParamFlag.SHOP_COMPANY,
					param.getCode());
			vo.setName(shopName);
			vo.setCode(param.getCode());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public List<Ana2ndData> getExportShopData(
			ShopCompanyOrientedModel shopCompanyOrientedModel,
			List<ShopCompanyVo> vos) {
		// TODO Auto-generated method stub
		AnaTable table = AnaTable.TB_ANA_DATA_2ND_GOV_D;
		if (YYYY_MM.equals(shopCompanyOrientedModel.getStartTimePattern())) {

			table = AnaTable.TB_ANA_DATA_2ND_GOV_M;
		}

		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", shopCompanyOrientedModel.getStartTime_F());
		params.put("endDate", shopCompanyOrientedModel.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("mainFlag", DimensionFlag.SHOPS_COMPANY.getCode());
		List<String> mainCodes = new ArrayList<String>();
		for (ShopCompanyVo vo : vos) {
			mainCodes.add(vo.getCode());
		}
		params.put("mainCodes", mainCodes);

		params.put("joinTableName", AnaTable.TB_CONF_DATA_PARAM);
		params.put("condition", " A.MAIN_CODE = B.CODE ");
		params.put("flag", ParamFlag.SHOP_COMPANY.getFlag());

		List<Ana2ndData> datas = ana2ndDataDao.listPort(params);

		return datas;
	}

	@Override
	public void toExportZhexianJson(JSONObject objectZhexian,
			List<Ana2ndData> shopDatas,
			ShopCompanyOrientedModel shopCompanyOrientedModel,
			List<ShopCompanyVo> vos, OrderByEnum orderByEnum) {
		Set<String> shopName = new HashSet<String>();
		Set<String> shopCode = new HashSet<String>();
		for (Ana2ndData an : shopDatas) {
			shopName.add(an.getMainName());
			shopCode.add(an.getMainCode());
		}
		JSONArray legendArray = new JSONArray();

		for (ShopCompanyVo vo : vos) {
			String name = vo.getName().trim();
			String code = vo.getCode().trim();
			if (StringUtils.isBlank(name)) {
				name = cdc.getName(ParamFlag.SHOP_COMPANY, vo.getCode());
			}
			if (StringUtils.isBlank(name)) {
				name = vo.getCode();
			}
			if (!shopName.contains(name)) {
				shopName.add(name);
			}
			if (!shopCode.contains(code)) {
				shopCode.add(code);
			}
		}
		for (String shop : shopName) {
			legendArray.add(shop);
		}
		// 按企业分组
		Map<String, List<Ana2ndData>> dataMap = new HashMap<String, List<Ana2ndData>>();
		for (Ana2ndData d : shopDatas) {
			String dimensionCode = d.getMainCode().trim();
			if (!dataMap.containsKey(dimensionCode)) {
				List<Ana2ndData> temp = new ArrayList<Ana2ndData>();
				temp.add(d);
				dataMap.put(dimensionCode, temp);
			}
			else {
				dataMap.get(dimensionCode).add(d);
			}
		}
		for (String shCode : shopCode) {
			if (!dataMap.containsKey(shCode)) {
				List<Ana2ndData> temp = new ArrayList<Ana2ndData>();

				dataMap.put(shCode, temp);
			}

		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		List<String> dateList = null;
		if (null != shopCompanyOrientedModel.getTimeSection()) {
			switch (shopCompanyOrientedModel.getTimeSection()) {

			case SIXMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 5),
						specifiedMonth);

				break;
			case NINEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 8),
						specifiedMonth);

				break;
			case TWELVEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 11),
						specifiedMonth);

				break;
			default:
				dateList = DateUtils.getDateList(
						shopCompanyOrientedModel.getStartTime_F(),
						shopCompanyOrientedModel.getEndTime_F());
				break;
			}
		}
		else {
			dateList = DateUtils.getDateList(
					shopCompanyOrientedModel.getStartTime_F(),
					shopCompanyOrientedModel.getEndTime_F());
		}
		JSONArray nameArray = new JSONArray();
		for (String date : dateList) {
			nameArray.add(date);
		}
		Iterator<Entry<String, List<Ana2ndData>>> iterator = dataMap.entrySet()
				.iterator();
		// 票数
		JSONArray arrayTotal = new JSONArray();
		// 金额
		JSONArray array1Total = new JSONArray();
		// 商品件数
		JSONArray array2Total = new JSONArray();

		while (iterator.hasNext()) {
			// 每个电商对应时间段的数据
			Entry<String, List<Ana2ndData>> item = iterator.next();
			String key = item.getKey();

			String name = cdc.getName(ParamFlag.SHOP_COMPANY, key);

			if (StringUtils.isBlank(name)) {
				name = key;
			}
			List<Ana2ndData> value = item.getValue();
			JSONObject ojb = new JSONObject();
			JSONObject ojb1 = new JSONObject();
			JSONObject ojb2 = new JSONObject();

			Map<String, Ana2ndData> temp = new TreeMap<String, Ana2ndData>();
			for (Ana2ndData a : value) {
				if (!temp.containsKey(a.getDateFlag())) {
					temp.put(a.getDateFlag(), a);
				}
			}

			// 补全数据
			for (String date : dateList) {
				if (null == temp.get(date)) {
					AnaImportData d = new AnaImportData();
					d.setSumNum(new BigDecimal(0));
					d.setSumPrice(new BigDecimal(0));
					d.setSumPkg(new BigDecimal(0));
					d.setAvgPrice(new BigDecimal(0));
					temp.put(date, d);
				}
			}

			JSONObject obj0 = new JSONObject();
			JSONObject obj01 = new JSONObject();
			JSONObject obj02 = new JSONObject();

			JSONObject obj2 = new JSONObject();
			JSONObject obj21 = new JSONObject();
			JSONObject obj22 = new JSONObject();

			JSONObject obj3 = new JSONObject();
			JSONObject obj3_1 = new JSONObject();
			JSONObject obj3_2 = new JSONObject();

			JSONObject obj31 = new JSONObject();
			JSONObject obj31_1 = new JSONObject();
			JSONObject obj31_2 = new JSONObject();
			JSONObject obj4 = new JSONObject();
			JSONObject obj41 = new JSONObject();
			JSONObject obj42 = new JSONObject();

			JSONObject obj5 = new JSONObject();
			JSONObject obj51 = new JSONObject();
			JSONObject obj52 = new JSONObject();
			String r = getRandom();
			String g = getRandom();
			String b = getRandom();
			String rgb = r + "," + g + "," + b;
			obj4.put("color", "rgba(" + rgb + ",0.5");
			obj41.put("color", "rgba(" + rgb + ",0.5");
			obj42.put("color", "rgba(" + rgb + ",0.5");
			obj3.put("show", true);
			obj3_1.put("show", true);
			obj3_2.put("show", true);
			obj31.put("show", true);
			obj31_1.put("show", true);
			obj31_2.put("show", true);

			obj5.put("label", obj31);
			obj51.put("label", obj31_1);
			obj52.put("label", obj31_2);

			obj2.put("label", obj3);
			obj2.put("areaStyle", obj4);

			obj2.put("color", "rgba(" + rgb + ",1");

			obj21.put("label", obj3_1);
			obj21.put("areaStyle", obj41);
			obj21.put("color", "rgba(" + rgb + ",1");

			obj22.put("label", obj3_2);
			obj22.put("areaStyle", obj42);
			obj22.put("color", "rgba(" + rgb + ",1");

			obj0.put("emphasis", obj5);
			obj0.put("normal", obj2);
			obj01.put("emphasis", obj51);
			obj01.put("normal", obj21);
			obj02.put("emphasis", obj52);
			obj02.put("normal", obj22);

			ojb.put("itemStyle", obj0);
			ojb.put("name", name);

			ojb.put("type", "line");

			

			ojb1.put("itemStyle", obj01);
			ojb1.put("name", name);

			ojb1.put("type", "line");

			

			ojb2.put("itemStyle", obj02);
			ojb2.put("name", name);

			ojb2.put("type", "line");

			

			Iterator<Entry<String, Ana2ndData>> iteratorTemp = temp.entrySet()
					.iterator();
			// 票数
			JSONArray array = new JSONArray();
			// 金额
			JSONArray array1 = new JSONArray();
			// 商品件数
			JSONArray array2 = new JSONArray();
			while (iteratorTemp.hasNext()) {
				Entry<String, Ana2ndData> i = iteratorTemp.next();
				Ana2ndData d = i.getValue();
				array.add(d.getSumNum());
				array1.add(d.getSumPrice_());
				array2.add(d.getSumPkg_());
			}
			ojb.put("data", array);
			ojb1.put("data", array1);
			ojb2.put("data", array2);
			arrayTotal.add(ojb);
			array1Total.add(ojb1);
			array2Total.add(ojb2);
		}
		switch (orderByEnum) {
		case SUM_NUM:
			objectZhexian.put("zhexiandata", arrayTotal);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		case SUM_PRICE:
			objectZhexian.put("zhexiandata", array1Total);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		case SUM_PKG:
			objectZhexian.put("zhexiandata", array2Total);
			objectZhexian.put("nameArray", nameArray);
			objectZhexian.put("legendArray", legendArray);
			break;
		default:
			break;
		}
	}

	@Override
	public List<Ana2ndData> getExportDatas(
			ShopCompanyOrientedModel shopCompanyOrientedModel,
			List<ShopCompanyVo> vos, DimensionFlag flag,Page page) {
		AnaTable table = AnaTable.TB_ANA_DATA_2ND_SHOP_D;
		if (YYYY_MM.equals(shopCompanyOrientedModel.getStartTimePattern())) {

			table = AnaTable.TB_ANA_DATA_2ND_SHOP_M;
		}

		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", shopCompanyOrientedModel.getStartTime_F());
		params.put("endDate", shopCompanyOrientedModel.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("code", vos.get(0).getCode());
		params.put("mainFlag", flag.getCode());

		List<Ana2ndData> datas = importDataDao.listShopData(params,page);

		return datas;
	}

	@Override
	public List<Ana2ndData> getNianDatas(
			ShopCompanyOrientedModel shopCompanyOrientedModel,
			List<ShopCompanyVo> vos, DimensionFlag flag) {
		AnaTable table = AnaTable.TB_ANA_DATA_IMPORT_2ND_SHOP_D;
		if (YYYY_MM.equals(shopCompanyOrientedModel.getStartTimePattern())) {

			table = AnaTable.TB_ANA_DATA_IMPORT_2ND_SHOP_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("startDate", shopCompanyOrientedModel.getStartTime_F());
		params.put("endDate", shopCompanyOrientedModel.getEndTime_F());
		List<String> codes = new ArrayList<String>();
		for (ShopCompanyVo vo : vos) {
			codes.add(vo.getCode());
		}
		params.put("codes", codes);
		params.put("mainFlag", flag.getCode());

		List<Ana2ndData> datas = importDataDao.listNianData(params);

		return datas;

	}

	@Override
	public void updateModel(ShopCompanyOrientedModel shopCompanyOrientedModel) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		if (null != shopCompanyOrientedModel.getTimeSection()) {
			switch (shopCompanyOrientedModel.getTimeSection()) {
			case SENVENDAY:

				break;
			case FIFTEENDAY:

				break;
			case THIRTYDAY:

				break;
			case SIXMONTH:

				shopCompanyOrientedModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 5));
				shopCompanyOrientedModel.setEndTime_F(specifiedMonth);
				shopCompanyOrientedModel.setStartTimePattern(YYYY_MM);

				break;
			case NINEMONTH:

				shopCompanyOrientedModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 8));
				shopCompanyOrientedModel.setEndTime_F(specifiedMonth);
				shopCompanyOrientedModel.setStartTimePattern(YYYY_MM);

				break;
			case TWELVEMONTH:
				shopCompanyOrientedModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 11));
				shopCompanyOrientedModel.setEndTime_F(specifiedMonth);
				shopCompanyOrientedModel.setStartTimePattern(YYYY_MM);
				break;
			default:
				break;
			}
		}
	}

}
